<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工权限管理</title>
<%@ include file="../../common/Style.jsp"%>
<script>
var dataGrid ;
$(document).ready(function(){
	parent.$.messager.progress('close'); 
	$('#dlg').dialog('close');
	 
	var params = $.serializeObject($('#basicDataForm'));

	dataGrid = $('#dataGrid').datagrid({
		url : 'userJSON!getRoles',
		queryParams: params,
		fit : true,
		border : false,
		checkOnSelect : false,
		selectOnCheck : false,
		singleSelect:true,
		nowrap : false,
		rownumbers : true,
		columns : [ [ {
				field : 'chainRoleTypeName',
				title : '角色名称	',
				width : 180
			    }, {
				field : 'isHead',
				title : '角色归属',
				align: 'center',
				formatter: function (value, row, index){
					if (value == 0)
						return '总部';
					else
						return '连锁店';
				},
				width : 140
			    }, {
					field : 'status',
					title : '状态',
					align: 'center',
					formatter: function (value, row, index){
						if (value == 0)
							return '正常';
						else
							return '锁定';
					},
					width : 120
				}, {			    	
				field : 'action',
				title : '编辑',
				align: 'center',
				width : 100,
				formatter : function(value, row, index) {
					var str = '';
					str += $.formatString('<a href="#" onclick="EditRole(\'{0}\');"><img border="0" src="{1}" title="编辑类别"/></a>', row.chainRoleTypeId, '<%=request.getContextPath()%>/conf_files/easyUI/themes/icons/update.png');
					return str;
				}
			}, {			    	
				field : 'roleF',
				title : '权限分配',
				align: 'center',
				width : 100,
				formatter : function(value, row, index) {
					var str = '';
					if(row.isHead == 1){
						str += $.formatString('<a href="#" onclick="editFunctions(\'{0}\');"><img border="0" src="{1}" title="权限分配"/></a>', row.chainRoleTypeId, '<%=request.getContextPath()%>/conf_files/easyUI/themes/icons/allocation2.png');
					}					
					return str;
				}
			}
				]],
	
	});

});

/**
 * 新增/修改角色信息
 */
function EditRole(roleId){
	var params = "formBean.basicData=role";
	if (roleId != 0)
	   params += "&formBean.basicDataId =" + roleId;

	$.modalDialog({
		title : "添加/更新角色",
		width : 540,
		height : 380,
		modal : false,
		draggable:false,
		href : 'basicData!preAddBasicData?' + params,
		
	});
}

/**
 * 根据角色ID查询角色权限树
 */
function editFunctions(roleId){
	
	$('#dlg').dialog('open');
	$('#roleId').val(roleId);
	$('#tt').tree({
	    url: 'basicDataJSON!selectFunctionTree'+"?formBean.roleId=" + roleId,  // 初始请求地址
	    method: 'get',
	    checkbox: true,
	    animate: true,
	    lines: true
	});	
}

/**
 * 修改角色权限
 */
function updateRoleFunctions(roleId){
	 
	 var checks = $('#tt').tree('getChecked'); 
     var nodes = $('#tt').tree('getChecked', 'indeterminate'); 
	    var s = '';
	    for(let a=0;a<checks.length;a++){
	    	if (s == ''){	    	 
				s += checks[a].id;
			}else{
				s += ',';
				s += checks[a].id;
			}
	    }
	    for(let i=0; i<nodes.length;i++){
	     	s += ',';
			s += nodes[i].id;
	    }
	  	var params =  "formBean.roleId=" + roleId + "&formBean.menuIds="+s;		
	    $.post("basicDataJSON!updateRoleFunctions",params, callBack,"json");
}

function callBack(data){
	$('#dlg').dialog('close')
	var response = data.response;
	$.messager.alert('提示',response.message,'warning');
}

</script>
</head>
<body>

	<div class="easyui-layout" data-options="fit : true,border : false">
		<div data-options="region:'north',border:false" style="height: 95px;">

			<s:form id="basicDataForm" name="basicDataForm" action=""
				theme="simple">
				<table width="98%" align="center" class="OuterTable">
			<tr>
				<td>
					<table width="100%" border="0">
										<tr>
											<td height="50" colspan="7">		
												<table width="100%" border="0">
													<input type="hidden" id="isInitialized" name="formBean.isInitialized" />
													<tr class="PBAOuterTableTitale">
														<td height="31" >系统角色权限：
															
														</td>
													</tr>
												<tr>
																			
									<td>		
										<a onclick="EditRole(0);" href="javascript:void(0);"
											class="easyui-linkbutton c6" style="height:25px;"
											data-options="iconCls:'icon-search'">新增角色</a>	
									</td>													
				

									</tr>
								</table>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
			</s:form>
		</div>
		
		
		<div data-options="region:'center',border:false">
			<table id="dataGrid" >
			</table>


    <div id="dlg" class="easyui-dialog" title="权限分配" data-options="iconCls:'icon-man', buttons: [{
                    text:'确认修改',
                    iconCls:'icon-edit',
                    handler:function(){                    	
                    	var roleId = $('#roleId').val();                                                
                        updateRoleFunctions(roleId);
                       
                    }
                },{
                    text:'返回',
                    iconCls:'icon-undo',
                    handler:function(){
                        $('#dlg').dialog('close')
                    }
                }]" style="width:360px;height:550px;top:30px">
    	
    		<ul id="tt" class="easyui-tree"></ul>
    	
        <input type="hidden" id="roleId" >
  
          
       
    </div>
			
		</div>
	</div>
</body>
</html>